package org.sith.taskrunner.gwt.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import org.sith.taskrunner.gwt.client.services.taskrunner.GetTasksToRunService;
import org.sith.taskrunner.gwt.client.utils.RootUtils;
import org.sith.taskrunner.gwt.client.widgets.RunTaskWidgetEditor;
import org.sith.taskrunner.gwt.shared.domains.RunTaskData;
import org.sith.taskrunner.gwt.shared.domains.WrapperData;

import java.util.List;

public class ButtonsWidget extends Composite {
    interface ButtonsPanelUiBinder extends UiBinder<Widget, ButtonsWidget> {
    }

    private static ButtonsPanelUiBinder ourUiBinder = GWT.create(ButtonsPanelUiBinder.class);
    @UiField
    Button runTaskButton;
    @UiField
    Button statisticsButton;
    @UiField
    Button manageTaskButton;

    public ButtonsWidget() {
        initWidget(ourUiBinder.createAndBindUi(this));

    }

    @UiHandler("runTaskButton")
    public void handleRunTaskButtonClick(ClickEvent event) {
        GetTasksToRunService.App.getInstance().getWrappersData(new AsyncCallback<List<WrapperData>>() {
            @Override
            public void onFailure(Throwable caught) {
                Window.alert(caught.toString());

            }

            @Override
            public void onSuccess(List<WrapperData> result) {
                RunTaskWidgetEditor widget = new RunTaskWidgetEditor();
                RunTaskData runTaskData = new RunTaskData();
                runTaskData.setWrappers(result);
                widget.edit(runTaskData);
                RootUtils.setWidgetToCenter(widget);
            }
        });

    }

    @UiHandler("manageTaskButton")
    public void handleManageTaskButton(ClickEvent event) {
        Window.alert("Manage Task");
    }

    @UiHandler("statisticsButton")
    public void handleStatisticsButtonClick(ClickEvent event) {
        Window.alert("Statisitcs");
    }

}